/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.controllers;

import com.insurance.cis.daos.EmployeeDao;
import com.insurance.cis.models.Employee;
import com.insurance.cis.models.Gender;
import com.insurance.cis.models.Group;
import com.insurance.cis.utils.CisConstants;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import com.insurance.cis.views.EmployeeJDialogCreationView;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author TruongLQ
 */
public class EmployeeCreationController {

    final EmployeeJDialogCreationView dialogCreationView;
    final Pattern pattern = Pattern.compile(CisConstants.DATE_PATTERN_COMPILE);
    EmployeeDao employeeDao = new EmployeeDao();

    public EmployeeCreationController(EmployeeJDialogCreationView aThis) {
        dialogCreationView = aThis;
    }

    public Employee validateEmployeeInfos() {

        String employeeID = dialogCreationView.getTxtEmpID().getText().trim();
        if (employeeID.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Employee ID must not be blank!");
            dialogCreationView.getTxtEmpID().requestFocus();
            return null;
        } else {
            if (employeeID.length() > 20) {
                MessageDialogUtils.dialogError(
                        dialogCreationView, "Employee ID must be maximum of 20 characters!");
                dialogCreationView.getTxtEmpID().requestFocus();
                return null;
            }
            if (employeeDao.checkIDExist(employeeID)) {
                MessageDialogUtils.dialogError(dialogCreationView,
                        "Employee ID aleady exists! Please choose another ID");
                dialogCreationView.getTxtEmpID().requestFocus();
                return null;
            }
        }

        String username = dialogCreationView.getTxtUserName().getText().trim();
        if (username.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Username must not be blank!");
            dialogCreationView.getTxtUserName().requestFocus();
            return null;
        } else {
            if (username.length() > 50) {
                MessageDialogUtils.dialogError(
                        dialogCreationView, "Username must be maximum of 50 characters!");
                dialogCreationView.getTxtUserName().requestFocus();
                return null;
            }
            if (employeeDao.checkUserNameExist(username)) {
                MessageDialogUtils.dialogError(dialogCreationView,
                        "Username aleady exists! Please choose another Username");
                dialogCreationView.getTxtUserName().requestFocus();
                return null;
            }
        }
        String password = String.valueOf(dialogCreationView.getTxtPwd().getPassword());
        if (password.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Password must not be blank!");
            dialogCreationView.getTxtPwd().requestFocus();
            return null;
        } else {
            if (password.length() > 50) {
                MessageDialogUtils.dialogError(
                        dialogCreationView, "Password must be maximum of 50 characters!");
                dialogCreationView.getTxtPwd().requestFocus();
                return null;
            }
        }
        String confirmPass = String.valueOf(dialogCreationView.getTxtConfimPwd().getPassword());
        if (confirmPass.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Confirm password must not be blank!");
            dialogCreationView.getTxtConfimPwd().requestFocus();
            return null;
        } else {
            if (!password.trim().equals(confirmPass)) {
                MessageDialogUtils.dialogError(
                        dialogCreationView, "Confirm password does not match!");
                dialogCreationView.getTxtConfimPwd().requestFocus();
                return null;
            }
        }

        String fullname = dialogCreationView.getTxtFullName().getText().trim();
        if (fullname.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogCreationView, "Fullname must not be blank!");
            dialogCreationView.getTxtFullName().requestFocus();
            return null;
        } else {
            if (fullname.length() > 50) {
                MessageDialogUtils.dialogError(
                        dialogCreationView, "Fullname must be maximum of 50 characters!");
                dialogCreationView.getTxtFullName().requestFocus();
                return null;
            }
        }

        Date dob;
        try {
            dob = validateDate(dialogCreationView.getTxtBirthday().getText().trim());
        } catch (ParseException ex) {
            dialogCreationView.getTxtBirthday().requestFocus();
            MessageDialogUtils.dialogError(dialogCreationView, "Birthday must be in format dd/mm/yyyy!");
            return null;
        }
        if (dob != null) {
            Date today = new Date();
            if (dob.after(today)) {
                MessageDialogUtils.dialogError(dialogCreationView, "Invalid birthday value!");
                dialogCreationView.getTxtBirthday().requestFocus();
                return null;
            }
        }

        if (!dialogCreationView.getRbtnMale().isSelected()
                && !dialogCreationView.getRbtnFemale().isSelected()) {
            MessageDialogUtils.dialogError(dialogCreationView, "You have to select Gender!");
            return null;
        }
        
        Group selectedGroup  =(Group)dialogCreationView.getCboCategories().getSelectedItem();
        if (selectedGroup.getId()==null) {
            MessageDialogUtils.dialogError(dialogCreationView, "You have to select Group!");
            dialogCreationView.getCboCategories().requestFocus();
            return null;
        }
        
        Employee savingEmployee = new Employee(employeeID, username, password, 
                fullname,  getGender(), dob, selectedGroup.getId());
        savingEmployee.setGroup(selectedGroup);
        return savingEmployee;
    }

    private String getGender() {
        if (dialogCreationView.getRbtnMale().isSelected()) {
            return Gender.MALE.getDesc();
        } else if (dialogCreationView.getRbtnFemale().isSelected()) {
            return Gender.FEMALE.getDesc();
        }
        return CisConstants.BLANK;
    }

    private Date validateDate(String date) throws ParseException {
        Date dateRet = null;

        if (!date.isEmpty()) {
            if (date.trim().length() != 10) {
                throw new ParseException("Date parse exception", 0);
            }
            Matcher matcher = pattern.matcher(date.trim());
            if (!matcher.matches()) {
                throw new ParseException("Date parse exception", 0);
            }
            dateRet = Utils.parseDate(date.trim(),
                    CisConstants.DATE_DD_MM_YYYY);
        }
        return dateRet;
    }
}
